package com.treasure.service.listener.process;

import com.alibaba.fastjson.JSON;
import com.treasure.service.commons.lang.util.ParserUtils;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.domain.AccessToken;
import com.treasure.service.domain.TUserInfo;
import com.treasure.service.dto.BizInformationPushForWechatOrSmsDto;
import com.treasure.service.enums.RedisKeys;
import com.treasure.service.enums.SmsTemplate;
import com.treasure.service.listener.enums.TemplateEnum;
import com.treasure.service.listener.template.TemplateData;
import com.treasure.service.listener.template.WechatPushTemplate;
import com.treasure.service.listener.template.DataItem;
import com.treasure.service.mapper.TUserInfoMapper;
import com.treasure.service.third.utils.RestClient;
import com.treasure.service.third.utils.SmsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
* @Description:    信用卡办理结果通知跑批
* @Author:         panxinwen
* @CreateDate:     2018/12/1 10:38
* @Version:        1.0
*/
@Component
public class BizInformationPushForWechatOrSmsProcess {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TUserInfoMapper tUserInfoMapper;

    @Autowired
    private RestClient restClient;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private SmsUtils smsUtils;
    /**
     * Todo 微信公众号模板消息推送或短信推送
     * @param bizInformationPushForWechatDto
     * @return
     */
    public String process(BizInformationPushForWechatOrSmsDto bizInformationPushForWechatDto){


        TUserInfo tUserInfo = tUserInfoMapper.selectByPrimaryKey(bizInformationPushForWechatDto.getUserId());

        int pushType = bizInformationPushForWechatDto.getPushType();

        if (StringUtil.isNotEmpty(tUserInfo.getOpenId()) && (pushType==2 || pushType==3)){

            pushWechatTemplate(bizInformationPushForWechatDto,tUserInfo.getOpenId());

        }
        if (StringUtil.isNotEmpty(tUserInfo.getPhonenumber())&&(pushType==1 || pushType==3)){
            pushSmsTemplate(bizInformationPushForWechatDto,tUserInfo.getPhonenumber());
        }

        return "";

    }

    private void pushWechatTemplate(BizInformationPushForWechatOrSmsDto bizInformationPushForWechatDto,String opendId){

        WechatPushTemplate creditCardTransactionResultTemplate = new WechatPushTemplate();
        creditCardTransactionResultTemplate.setTouser(opendId);
        creditCardTransactionResultTemplate.setUrl(TemplateEnum.getUrl(bizInformationPushForWechatDto.getBizType()));
        creditCardTransactionResultTemplate.setTemplate_id(TemplateEnum.getTemplateId(bizInformationPushForWechatDto.getBizType()));

        switch (bizInformationPushForWechatDto.getBizType()){
            case 101:
                break;
            case 2:
                break;
            default:
                break;
        }
        TemplateData creditCardTransactionResultItem= new TemplateData();
        creditCardTransactionResultItem.setFirst(new DataItem("恭喜您成功获得1000000元返利！","#173177"));
        creditCardTransactionResultItem.setKeyword1(new DataItem("SCYD10001","#173177"));
        creditCardTransactionResultItem.setKeyword2(new DataItem("中信银行白金卡","#173177"));
        creditCardTransactionResultItem.setRemark(new DataItem("生财有道-您的生财之道！","#173177"));
        creditCardTransactionResultTemplate.setData(creditCardTransactionResultItem);

        AccessToken accessToken = JSON.parseObject((String) redisTemplate.opsForHash().get(RedisKeys.ACCESS_TOKEN.build(""), "access_token"),AccessToken.class);
        String result = restClient.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken.getAccessToken(),creditCardTransactionResultTemplate,String.class);
        logger.info("微信消息推送返回结果：{}",result);
    }

    private void pushSmsTemplate(BizInformationPushForWechatOrSmsDto bizInformationPushForWechatDto,String phonenumber){

        String content = ParserUtils.parse0(SmsTemplate.getTemplate(bizInformationPushForWechatDto.getBizType()),bizInformationPushForWechatDto.getSocket());
        smsUtils.sendSmsTemp(phonenumber,content);

        logger.info("发送短信：{}",content);
    }

}
